package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.MqGeneralReply;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.utils.ReceiverUtils;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.entity.VcomRequestRecord;
import com.wtwd.campus.vcom.service.RequestRecordService;
import com.wtwd.campus.vcom.service.VcomDeviceInfoService;
import com.wtwd.campus.vcom.utils.VcomPushUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/10 15:09
 * @Description:
 */
@Service
@MsgType(value = Constants.VCOM_NOTICE_MESSAGE)
public class VcomNoticeMessageReceiver implements IReceiverHandler{

    @Autowired
    private RequestRecordService requestRecordService;
    @Autowired
    private VcomPushUtils vcomPushUtils;
    @Autowired
    private VcomDeviceInfoService vcomDeviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;
    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqGeneralReply mqGeneralReply = (MqGeneralReply) mqMessage.getObject();
        String imei = mqGeneralReply.getImei();
        String commandNumber = mqGeneralReply.getCommandNumber();
        Map<String,Object> map = requestRecordService.getRequestRecordByCondition(imei,commandNumber, Constant.COMMAND_TYPE_MESSAGE);
        if(map != null){
            // 推送requestId结果
            vcomPushUtils.pushAndUpdateRequestStatus((int)map.get("id"),(String)map.get("request_id"),Constant.STATUS_COMPLETE,Constant.COMMAND_TYPE_MESSAGE,imei);
            VcomRequestRecord nextSendCommand = requestRecordService.getNextSendCommand(imei, (String)map.get("request_id"), Constant.COMMAND_TYPE_MESSAGE);
            if(nextSendCommand != null){
                vcomDeviceInfoService.sendMessage(nextSendCommand);
            }
            String delKey = Constants.VCOM_NOTICE_MESSAGE.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.VCOM_NOTICE_MESSAGE);
            JSONObject contant = new JSONObject();
            contant.put("设备号", imei);
            contant.put("设备时间",mqGeneralReply.getResult());
            deviceLog.setCmd(contant.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        }
    }

}
